/**
 * Created by zhong on 2017/10/24.
 *
 * (resolve) =>{require(['../components/test.vue'],resolve)}
 *
 */
import Vue from 'vue'
import store from './vuex'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

// 定义全局组件
import zmodal from './component/common/modal.vue'
import zbanner from './component/common/banner.vue'
import zfooter from './component/common/footer.vue'
import zhistory from './component/common/goback.vue'
import zlogo from './component/common/logo.vue'
import backhome from './component/common/back-home.vue'
import phead from './component/common/pre-header.vue'

Vue.component('z-modal', zmodal)
Vue.component('z-banner', zbanner)
Vue.component('z-footer', zfooter)
Vue.component('z-history', zhistory)
Vue.component('z-logo', zlogo)
Vue.component('z-home', backhome)
Vue.component('pheader', phead)

// 首页
const short = (resolve) => {require(['./pages/short.vue'],resolve)}
const home = (resolve) => {require(['./pages/home/home.vue'],resolve)}
const search = (resolve) => {require(['./pages/home/search.vue'],resolve)}
const article = (resolve) => {require(['./pages/home/article.vue'],resolve)}

//发现
const discover = (resolve) => {require(['./pages/discover/discover.vue'],resolve)}
const activities = (resolve) => {require(['./pages/discover/activities.vue'],resolve)}
const activity = (resolve) => {require(['./pages/discover/activity.vue'],resolve)}
const columns = (resolve) => {require(['./pages/discover/columns.vue'],resolve)}
const column = (resolve) => {require(['./pages/discover/column.vue'],resolve)}
const single = (resolve) => {require(['./pages/discover/single.vue'],resolve)}
const activityPlace = (resolve) => {require(['./pages/discover/place.vue'],resolve)}
const nopay = (resolve) => {require(['./pages/discover/nopay.vue'],resolve)}

//关注
const authors = (resolve) => {require(['./pages/author/link.vue'],resolve)}
const authorList = (resolve) => {require(['./pages/author/authors.vue'],resolve)}
const author = (resolve) => {require(['./pages/author/author.vue'],resolve)}

//订单支付相关
const createOrder = (resolve) => {require(['./pages/order/create-order.vue'],resolve)}
const payment = (resolve) => {require(['./pages/order/payment.vue'],resolve)}
const pay = (resolve) => {require(['./component/pay/pay.vue'],resolve)}
const ticket = (resolve) => {require(['./pages/order/ticket.vue'],resolve)}

const weChatAct = (resolve) => {require(['./pages/order/weChatAct.vue'],resolve)} //活动签到微信跳转
const weChatAdmin = (resolve) => {require(['./pages/order/weChatAdmin.vue'],resolve)} //申请成为管理微信跳转
const applyAdmin = (resolve) => {require(['./pages/order/applyAdmin.vue'],resolve)} //申请成为管理员页面
const activitySign = (resolve) => {require(['./pages/order/sign.vue'],resolve)} //活动签到页
const activityTicket = (resolve) => {require(['./pages/order/eleTicket.vue'],resolve)} //活动电子票
const alipay = (resolve) => {require(['./pages/order/alipay.vue'],resolve)} //支付宝支付

// 登录注册
const login = (resolve) => {require(['./pages/login/login.vue'],resolve)} //密码登录页面
const forgotPwd = (resolve) => {require(['./pages/login/forgotPwd.vue'],resolve)} //忘记密码页
const bindPhoneNo = (resolve) => {require(['./pages/login/bindPhoneNo.vue'],resolve)} //绑定手机页
const loginPhoneNo = (resolve) => {require(['./pages/login/loginPhoneNo.vue'],resolve)} //动态验证码登录页
const register = (resolve) => {require(['./pages/login/register.vue'],resolve)} //用户注册页
const wxlogin = (resolve) => {require(['./pages/login/wxlogin.vue'],resolve)} //第三方授权微信跳转页
const wblogin = (resolve) => {require(['./pages/login/wblogin.vue'],resolve)} //第三方授权微博跳转页
const qqlogin = (resolve) => {require(['./pages/login/qqlogin.vue'],resolve)} //第三方授权qq跳转页
const wxbaselogin = (resolve) => {require(['./pages/login/wxbaselogin.vue'],resolve)}
const wxBind = (resolve) => {require(['./pages/login/wxBind.vue'],resolve)}
const wxcode = (resolve) => {require(['./pages/login/wxcode.vue'],resolve)}

//个人中心
const personalHome = (resolve) => {require(['./pages/mycenter/personal-home.vue'],resolve)}
const setting = (resolve) => {require(['./pages/mycenter/setting.vue'],resolve)}
const sponsor = (resolve) => {require(['./pages/mycenter/sponsor.vue'],resolve)}
const vip = (resolve) => {require(['./pages/mycenter/vip.vue'],resolve)}
const myOrders = (resolve) => {require(['./pages/mycenter/myorders.vue'],resolve)}
const mycollect = (resolve) => {require(['./pages/mycenter/mycollect.vue'],resolve)}
const mynews = (resolve) => {require(['./pages/mycenter/news.vue'],resolve)}
const profile = (resolve) => {require(['./pages/mycenter/profile.vue'],resolve)}

// 404错误页面
const error = (resolve) => {require(['./pages/error.vue'],resolve)}
const ssss=res=>{require(['./component/common/search.vue'],res)}
// 创建路由器实例，配置路由规则
const routers = [
    {path:'/ssss',component:ssss},
    {path: '/d/:shortId', component: short},
    {name: 'home',path:'/',component: home, meta: {keepAlive: true}},
    {name: 'articles',path:'/index/:typeId',component: home, meta: {keepAlive: true}},
    {name: 'article',path:'/article/:artId',component: article},

    {name: 'search',path:'/search/:type/:query',component: search},

    {name: 'discover',path:'/discover',component: discover, meta: {keepAlive: true}},
    {name: 'activities',path:'/activities',component: activities},
    {name: 'activity',path:'/activity/:actId',component: activity},
    {name: 'columns',path:'/columns',component: columns, meta: {keepAlive: true}},
    {name: 'activityPlace',path:'/activity/place',component:activityPlace},
    {name: 'column',path:'/columns/:colId',component: column},
    {name: 'single',path:'/singles/:id/:type',component: single},
    {name: 'nopay',path:'/nopays/:id',component: nopay}, //未购买时知识页面


    {name: 'authors',path:'/link',component: authors, meta: {keepAlive: true}},
    {name: 'authorList',path:'/authors',component: authorList, meta: {keepAlive: true}},
    {name: 'author',path:'/authors/:autype/:autId',component: author},

    {name: 'err',path:'/s/404',component: error}, //错误跳到404页面
    
    // 登录注册模块
    {name: 'password', path:'/login/password',component: login}, // 密码登录页
    {name: 'find', path:'/passwords/phone',component: forgotPwd}, // 密码找回页
    {name: 'bind', path:'/bind',component: bindPhoneNo}, // 绑定手机页面
    {name: 'setPhone', path:'/setting/phone',component: bindPhoneNo}, // 绑定手机页面
    {name: 'login', path:'/login',component: loginPhoneNo}, // 动态验证码登录
    {name: 'register', path:'/register',component: register}, // 注册页面
    {name:'wxbaselogin',path:'/wxbaselogin',component: wxbaselogin}, // 微信跳转页
    {name: 'wxlogin', path:'/wxlogin',component: wxlogin}, // 微信跳转页
    {name: 'wblogin', path:'/wblogin',component: wblogin}, // 微博跳转页
    {name: 'qqlogin', path:'/qqlogin',component: qqlogin}, // QQ跳转页
    {name:'wxBind',path:'/wxBind',component: wxBind}, //微信绑定
    {name:'wxcode',path:'/wxcode',component: wxcode}, //微信兑换码内绑定

    // 订单支付相关
    {name:'createOrder', path:'/order/:type/:comNo',component:createOrder},
    {name:'payment',path:'/payment/:orderNo/:type',component: payment}, //订单待支付页面
    {name:'pay',path:'/pay/:orderNo/:type',component:pay},
    {name:'pay2',path:'/pay/pay/',component:pay},
    {name:'ticket',path:'/ticket/:orderNo',component:ticket},

    {name:'weChatAct',path:'/weChatAct',component: weChatAct}, // 微信跳转页(活动签到)
    {name:'weChatAdmin',path:'/weChatAdmin',component: weChatAdmin},//申请成为管理微信跳转
    {name:'applyAdmin',path:'/applyAdmin',component:applyAdmin}, // 申请成为管理员页面
    {name: 'activitySign',path:'/activitySign/:code/:actId/:ticketNo',component:activitySign},
    {name: 'activityTicket',path:'/mytickets/:randomId',component:activityTicket}, //活动电子票
    {name: 'alipay',path:'/alipay',component: alipay},

    // 个人中心
    {name:'mycenter', path:'/mycenter',component:personalHome},
    {name:'setting',path:'/setting',component:setting},//个人设置
    {name:'myOrders',path:'/orders',component:myOrders},//个人设置
    {name:'sponsor',path:'/sponsor',component:sponsor},//个人设置
    {name:'mycollect',path:'/collect',component:mycollect},//个人设置
    {name:'mynews',path:'/message',component:mynews},//个人设置
    {name:'vip',path:'/vip',component:vip},//个人设置
    {name:'profile',path:'/profile',component:profile},//个人设置

]

const router = new VueRouter({
    mode: 'history',
    base: __dirname,
    routes:routers
})

let isClient = typeof window != 'undefined' ? true : false
router.beforeEach((to, from, next) => {
    if(isClient) {
        let scroll = document.getElementById("mescroll")
        // console.log(from,scroll.scrollTop)
    }
    next()
})
router.afterEach((to, from, next) => {
    if(isClient) {
        let scroll = document.getElementById("mescroll")
        // scroll.scrollTop = 100
        // console.log(to,scroll.scrollTop)
        function checkUrl() {
            if(to.name=='login' || to.name=='password' || to.name=='find' || to.name=='bind' || to.name=='register' || to.name=='setPhone') return false
            else if(to.name=='wxbaselogin' || to.name=='wxlogin' || to.name=='wblogin' || to.name=='qqlogin' || to.name=='wxbaselogin') return false
            else if(to.name=='weChatAct' || to.name=='weChatAdmin' || to.name=='wxBind' || to.name=='wxcode' || to.name=='payOk') return false
            else if(to.name=='pay' || to.name=='pay2' || to.name=='applyAdmin') return false
            else{ return true}
        }
        if( checkUrl()) {
            let i = store.state.fromPath.length-1
            // console.log('from',store.state.fromPath[i],'to',to.path);
            if(store.state.fromPath[i] != to.path){
                if(to.name != 'home'){
                    if (i<2 || store.state.fromPath[i - 1] != to.path){
                        // console.log('记录frompath');
                        store.state.fromPath.push(to.path);
                    }
                }else if(store.state.fromPath[i] !='home'){
                    store.state.fromPath.push('home');
                }
            }

            if (i<1 || store.state.fromPath[i - 1] != to.path) {
                // console.log('增加state');
                window.history.pushState({}, '', window.location.href)
            } else if (store.state.fromPath[i - 1] == to.path) {
                // console.log('删除frompath',store.state.fromPath);
                store.state.fromPath.pop();
            }
            // console.log(11,String(store.state.fromPath));
        }
    }
});

export default router